package com.dewod.producesales.utils.request;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.dewod.producesales.model.request.EnableOrderQuery;
import com.dewod.producesales.model.request.OrderDelivery;
import com.dewod.producesales.model.request.SnSubmit;
import com.dewod.producesales.model.request.SnValidate;
import com.dewod.producesales.utils.HttpUtil;
import com.dewod.producesales.model.response.base.ResponseData;
import com.google.firebase.crashlytics.buildtools.reloc.org.apache.http.message.BasicNameValuePair;

import org.json.JSONException;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class RequestApiUtil extends RequestBaseUtil{

    /**
     * 设备注册
     * @param url
     * @param callBack
     */
    public static void register(String url, CallBack<Boolean> callBack) {
        new Thread(() -> {
            try {
                ApiEnums apiEnum = ApiEnums.REGISTER;
                String responseData = HttpUtil.get(url, apiEnum.getUrlSuffix(), null, null, null);
                if (callBack != null) {
                    ResponseData ajax = JSON.parseObject(responseData,  ResponseData.class);
                    callBack.action(ajax.getSuccess());
                }
            } catch (Exception e) {
                Log.e("request", "设备注册异常==================>" + e);
                ToastUtils.showLong(String.format("%s%s", "设备注册异常==================>", e));
                if (callBack != null) {
                    try {
                        callBack.action(false);
                    } catch (JSONException ex) {
                        throw new RuntimeException(ex);
                    }
                }
            }
        }).start();
    }

    public static void validateOrderNo(SnValidate form, CallBack<ResponseData> callBack) {
        new Thread(() -> {
            try {
                ApiEnums apiEnum = ApiEnums.VALIDATE_ORDER_NO;
                String responseData = doRequest(apiEnum, form);
                if (callBack != null) {
                    ResponseData ajax = JSON.parseObject(responseData,  ResponseData.class);
                    callBack.action(ajax);
                }
            } catch (Exception e) {
                Log.e("request", "请求订单号验证==================>" + e);
                ToastUtils.showLong(String.format("%s%s", "请求订单号验证==================>", e));
            }
        }).start();
    }

    public static void validateSn(SnValidate form, CallBack<ResponseData> callBack) {
        new Thread(() -> {
            try {
                ApiEnums apiEnum = ApiEnums.VALIDATE_ORDER_NO;
                String responseData = doRequest(apiEnum, form);
                if (callBack != null) {
                    ResponseData ajax = JSON.parseObject(responseData,  ResponseData.class);
                    callBack.action(ajax);
                }
            } catch (Exception e) {
                Log.e("request", "请求订单号验证==================>" + e);
                ToastUtils.showLong(String.format("%s%s", "请求订单号验证==================>", e));
            }
        }).start();
    }

    public static void formSubmit(SnSubmit form, CallBack<ResponseData> callBack) {
        new Thread(() -> {
            try {
                ApiEnums apiEnum = ApiEnums.SUBMIT_SN;
                String responseData = doRequest(apiEnum, form);
                if (callBack != null) {
                    ResponseData ajax = JSON.parseObject(responseData,  ResponseData.class);
                    callBack.action(ajax);
                }
//                Log.i("request", "请求订单号验证==================>" + responseData);
            } catch (Exception e) {
                Log.e("request", "请求订单号验证==================>" + e);
                ToastUtils.showLong(String.format("%s%s", "请求订单号验证==================>", e));
            }
        }).start();
    }

    public static void cancelOne(SnValidate form, CallBack<ResponseData> callBack) {
        new Thread(() -> {
            try {
                ApiEnums apiEnum = ApiEnums.CANCEL_ONE;
                String responseData = doRequest(apiEnum, form);
                if (callBack != null) {
                    ResponseData ajax = JSON.parseObject(responseData,  ResponseData.class);
                    callBack.action(ajax);
                }
            } catch (Exception e) {
                Log.e("request", "SN取消使用==================>" + e);
                ToastUtils.showLong(String.format("%s%s", "SN取消使用异常==================>", e));
            }
        }).start();
    }

    public static void cancel(SnSubmit form, CallBack<ResponseData> callBack) {
        new Thread(() -> {
            try {
                ApiEnums apiEnum = ApiEnums.CANCEL;
                String responseData = doRequest(apiEnum, form);
                if (callBack != null) {
                    ResponseData ajax = JSON.parseObject(responseData,  ResponseData.class);
                    callBack.action(ajax);
                }
            } catch (Exception e) {
                Log.e("request", "合同订单取消使用==================>" + e);
                ToastUtils.showLong(String.format("%s%s", "合同订单取消使用异常==================>", e));
            }
        }).start();
    }

    public static void getEnableOrder(EnableOrderQuery form, CallBack<ResponseData> callBack) {
        new Thread(() -> {
            try {
                ApiEnums apiEnum = ApiEnums.ENABLE_ORDER;
                String responseData = doRequest(apiEnum, form);
                Log.i("request", "合同查询结果==================>" + responseData);
                if (callBack != null) {
                    ResponseData ajax = JSON.parseObject(responseData,  ResponseData.class);
                    callBack.action(ajax);
                }
            } catch (Exception e) {
                Log.e("request", "合同查询结果==================>" + e);
                ToastUtils.showLong(String.format("%s%s", "合同查询异常==================>", e));
            }
        }).start();
    }

    public static void updateRemark(OrderDelivery form, CallBack<ResponseData> callBack) {
        new Thread(() -> {
            try {
                ApiEnums apiEnum = ApiEnums.UPDATE_ORDER;
                String responseData = doRequest(apiEnum, form);
                Log.i("request", "货运信息更新结果==================>" + responseData);
                if (callBack != null) {
                    ResponseData ajax = JSON.parseObject(responseData,  ResponseData.class);
                    callBack.action(ajax);
                }
            } catch (Exception e) {
                Log.e("request", "货运信息更新结果==================>" + e);
                ToastUtils.showLong(String.format("%s%s", "货运信息更新结果==================>", e));
            }
        }).start();
    }

}
